package gui;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Vector;

import backproped.ContinuousNeuronLayerClassifier;


/* 
 * Class
 * This class provides functionality to save objects to file and retrieve the data 
 * 
 * */

public abstract class Filesystem {

        /**
         * @param args
         * @throws IOException 
         */
        public static void main(String[] args){
                // Main function is a test case 
                /*
                int[][] outputArray = { {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                        {0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0},
                        {0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0},
                        {0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0},
                        {0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0},
                        {0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0},
                        {0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0},
                        {0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0},
                        {0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0},
                        {0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0},
                        {0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0},
                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}       }; 
                
                NeuralNetworkSet newSet = new NeuralNetworkSet(null, 0);
                
                NeuralNetworkSet readSet = new NeuralNetworkSet(null, 0);
                
                newSet.setInput(outputArray);
                newSet.setDesired(1);
                
                String directory = "C:\\defaultProperties.prop";
                System.out.println("being writing data @ " + directory);

                
                writeData(directory,newSet);
                
                System.out.println("begin reading data @ " + directory);
                
                
                readSet = readData(directory);
                
                System.out.println("reading input" + printArray(readSet));
                */
        }

        public static boolean writeData(String destinationFilename,NeuralNetworkSet readSet)
        {
                boolean success = false;
                
                try {
					destinationFilename = determineFilename(new File(destinationFilename));
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				System.out.println("writeDataset\t"+ destinationFilename);
                try {
                        FileWriter writer = new FileWriter(destinationFilename);
                        for (int[] y : readSet.getInput()) {
                                for (int x : y) {
                                        writer.write(x+",");
                                }
                                writer.write("\n");
                        }
                        // write desired value
                        writer.write(readSet.getDesired()+"!");
                        writer.close();
                        success = true;
                } catch (Exception e) {
                        System.out.println(e);
                }
                return success;

        }
        
        public static NeuralNetworkSet readData(String originFilename)
        {

                NeuralNetworkSet inputSet = new NeuralNetworkSet();
                int[][] outputArray = inputSet.getInput();
                // ---------------------

                ArrayList<ArrayList<Integer>> y = new ArrayList<ArrayList<Integer>>();
                ArrayList<Integer> x = new ArrayList<Integer>();
                
                String input = "";
                                
                try {
                        FileReader reader = new FileReader(originFilename);
                        int read = 0;
                        while (read != -1) {
                                read = reader.read();
                                if (read != -1) {
                                        if (read == (int)(','))  {
                                                x.add(Integer.valueOf(input));
                                                input = ""; //reset this
                                        } else if (read == (int)('\n')) {
                                                y.add(x);
                                                x = new ArrayList<Integer>();
                                                input = ""; //reset this
                                        } else if (read == (int)('!')) {
                                                inputSet.setDesired(Integer.valueOf(input));
                                        } else {
                                                input += (char) read;
                                        }
                                }
                        }
                        reader.close();
                        //translate from the array list to the array
                    outputArray = new int[y.size()][y.get(0).size()];
                        for (int i = 0; i < y.size(); i++) {
                                for (int j = 0; j < y.get(0).size(); j++) {
                                        outputArray[i][j] = y.get(i).get(j);
                                }
                        }
                        
                        y.clear();
                        x.clear();
                } catch (Exception e) {
                        System.out.println(e);
                }
                
                inputSet.setInput(outputArray);
                
                
                // ---------------------
                
                
                
        return inputSet;

        }
        
        public static String determineFilename(File dir) throws IOException{
    		String filename = dir.getName();
    		boolean filenameExists = false;

        	String returnString = null;
        	int maxIterator = -1;
    		String[] directoryListing = (new File(dir.getPath().substring(0, dir.getPath().length() - filename.length()))).list();
    		
    		Vector<String> files = new Vector<String>();		
    	   		//Sort files from directories

    			for(int i=0; i<directoryListing.length; i++){
	    			
	    			File file = new File(dir, directoryListing[i]);
	    			
	    			if(file.isDirectory()){
	    				// Do nothing if a Directory
	    			} else {
		    	    			
	    				//System.out.print(file.getName().toString().charAt(file.toString().indexOf('-')+1)+"\t"+filename.charAt((filename.indexOf('-')+1)));
    	    			if (file.getName().toString().charAt(file.getName().toString().indexOf('-')+1) == (filename.charAt((filename.indexOf('-')+1))))
    	    			{
    	    				//System.out.println(file.getName().toString());
    	    				if(!file.getName().toString().endsWith(".jpg") || !file.getName().toString().endsWith(".gif") || !file.getName().toString().endsWith(".png"))
    	    				{
    	    					if (maxIterator == -1)
	    	    				{ // Not assigned yet
	    	    					maxIterator = Integer.parseInt(""+file.getName().toString().charAt(file.getName().toString().lastIndexOf('-')+1));    	    				
	    	    				} else if( maxIterator < file.getName().toString().charAt(file.getName().toString().lastIndexOf('-')+1)) {
	    	    					maxIterator = Integer.parseInt(""+file.getName().toString().charAt(file.getName().toString().lastIndexOf('-')+1));
	    	    				}
    	    				}
    	    			}
	    			}	    			    			
	    		}
	
	    		maxIterator++; // add 1 to the iterator to create a unique filename
	    		returnString = filename +"-"+maxIterator;
		    	
	    	if (dir.getAbsolutePath().contains("noise_samples"))
	    	{
	    		returnString = returnString.substring(0, returnString.length() - 2);
	    	}
	    	System.out.println("new File \t"+returnString);
    		return returnString;
    	}

        public static Vector<String> getDirectoryFilesContaining(String findString, File directory)
        {
        	if(directory==null)
        	{
        		directory = new File(System.getProperty("user.dir") + "\\src\\samples\\");
        		System.out.println("USERDIR\t"+ System.getProperty("user.dir") + "\\src\\samples\\train-0-0");
        	}
        	
        	String[] directoryListing = directory.list();
        	Vector<String> files = new Vector<String>();	
        	
        	for(int i=0; i<directoryListing.length; i++){
    			
    			File file = new File(directory, directoryListing[i]);
    			
    			if(file.isDirectory()){
    				// Do nothing if a Directory
    			} else {
    				
    				if(file.getName().contains(findString))
    				{
    					System.out.println(file.getAbsolutePath());
    					files.add(file.getAbsolutePath());	
    				}
    			}
        	}
        	return files;
        }

}
